package com.ourteam.workspace.api;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FalseFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;

import com.ourteam.workspace.domain.WorkspaceResourceBean;

public class SourceCodePackageNodebuilder extends AbstractWorkspaceResourceTreeNodeBuilder<WorkspaceResourceBean> {

	@Override
	public WorkspaceResourceFileBean[] getTreeNodeObjects(WorkspaceResourceBean parentObject) throws Exception {
		final File workSpacePath = new File(getWorkspaceRootDir(),
				parentObject.getWorkspacePath() + SystemUtils.FILE_SEPARATOR + parentObject.getResourcePath());

		if (workSpacePath.exists() == false) {
			return new WorkspaceResourceFileBean[0];
		}

		final List<File> packageFolders = new ArrayList<File>();

		File[] folders = workSpacePath.listFiles(new FileFilter() {

			@Override
			public boolean accept(File pathname) {
				return pathname.isDirectory();
			}
		});

		File[] files = workSpacePath.listFiles(new FileFilter() {

			@Override
			public boolean accept(File pathname) {
				return pathname.isFile() && pathname.isHidden() == false;
			}
		});

		if (ArrayUtils.isNotEmpty(files)) {

			packageFolders.add(workSpacePath);
		}
		for (int i = 0; i < folders.length; i++) {
			getPackages(folders[i], packageFolders);
		}

		final WorkspaceResourceFileBean[] resourceFileBeans = new WorkspaceResourceFileBean[packageFolders.size()];

		for (int i = 0; i < resourceFileBeans.length; i++) {
			final File packageFile = packageFolders.get(i);
			resourceFileBeans[i] = new WorkspaceResourceFileBean(
					workspaceService.getWorkspaceById(parentObject.getWorkspaceId()), packageFile) {

				@Override
				public String getName() {
					String packageName = StringUtils.replace(
							StringUtils.substringAfter(packageFile.getPath(), workSpacePath.getPath() + SystemUtils.FILE_SEPARATOR), SystemUtils.FILE_SEPARATOR, ".");
					if (StringUtils.isEmpty(packageName)) {
						packageName = "(default package)";
					}
					return packageName;
				}

			};
		}

		return resourceFileBeans;
	}

	@Override
	public WorkspaceResourceBean converToParentObject(Object parent) throws Exception {
		return workspaceService.getWorkspaceResourceById(Long.parseLong(parent.toString()));
	}

	public String getTag(WorkspaceResourceFileBean childObject) throws Exception {

		return String.valueOf(FileUtils
				.listFiles(childObject.getResourceFile(), TrueFileFilter.INSTANCE, FalseFileFilter.INSTANCE).size());

	}

	protected void getPackages(File folder, List<File> packageDir) {

		File[] folders = folder.listFiles(new FileFilter() {

			@Override
			public boolean accept(File pathname) {
				return pathname.isDirectory();
			}
		});

		File[] files = folder.listFiles(new FileFilter() {

			@Override
			public boolean accept(File pathname) {
				return pathname.isFile() && pathname.isHidden() == false;
			}
		});

		if (ArrayUtils.isNotEmpty(files)) {

			packageDir.add(folder);
		}
		for (int i = 0; i < folders.length; i++) {
			getPackages(folders[i], packageDir);
		}

	}

	@Override
	public String getNodeDefineId(WorkspaceResourceFileBean childObject) throws Exception {

		return "sourceCodePackageNode";
	}

}
